<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>使用闭包获取区间的商品</title>
</head>

<body>

</body>
<script>
  "use strict"; {
    let arr = [2, 122, 200, 1298, 123, 12, 33, 4545];
    let get1 = arr.filter((item, index, arrs) => {
      return item >= 2 && item <= 15;
    });
    let get2 = arr.filter((item, index, arrs) => {
      return item >= 100 && item <= 200;
    });
    console.log(get1, get2);
    // 这时候可以把两方法通过闭包来合并起来
    const between = (value1, value2) => {
      /* 
        将该函数返回给filter,即arr.filter((item,index,arr)=>{
          return item >= value1 && item <= value2;  
        });
        value1,value2并不会销毁，形成了闭包。
      */
      return (item, index, arr) => {
        return item >= value1 && item <= value2;
      };
    };
    console.log(arr.filter(between(2, 15)));
    console.log(arr.filter(between(100, 200)));

  };
</script>

</html>